package com.googlecode.mailnet.emf.netmodel.visualization.highrisk;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Shape;
import org.eclipse.swt.graphics.Color;

import com.googlecode.mailnet.emf.netmodel.simulation.NodeStatistics;
import com.googlecode.mailnet.emf.netmodel.simulation.SimulationContext;
import com.googlecode.mailnet.emf.netmodel.visualization.ShapeDecorator;
import com.googlecode.mailnet.info.INodeInfo;

public class HighRiskNodeDecorator extends ShapeDecorator {
	
	INodeInfo nodeInfo;
	SimulationContext context;
	
	public HighRiskNodeDecorator(INodeInfo nodeInfo, SimulationContext context) {
		this.nodeInfo = nodeInfo;
		this.context = context;
	}
	
	@Override
	public boolean outlineShape(Shape shape, Graphics g) {
		NodeStatistics nodeStat = context.getStatistics().getNodeStats(nodeInfo.getNode());
		double dataTotal = nodeStat.dataFailed + nodeStat.dataTransmitted;
		double risk = ((double)nodeStat.dataFailed) / dataTotal;
		if (risk > 0.05) risk = 0.05;
		risk *= 20.0;
		double importance = nodeStat.importance;

		long colorBrightness = Math.round(importance * risk * 85.0);
		Color color = new Color(null, 170 + (int)colorBrightness, 170 - (int)colorBrightness * 2, 170 - (int)colorBrightness * 2);
		shape.setForegroundColor(color);
		shape.setLineStyle(nodeInfo.isBroken() ? Graphics.LINE_DASH : Graphics.LINE_SOLID);
		shape.setLineWidth(1);
		return false;
	}

	public boolean fillShape(Shape shape, Graphics g) {
		
		NodeStatistics nodeStat = context.getStatistics().getNodeStats(nodeInfo.getNode());
		double dataTotal = nodeStat.dataFailed + nodeStat.dataTransmitted;
		double risk = ((double)nodeStat.dataFailed) / dataTotal;
		if (risk > 0.05) risk = 0.05;
		risk *= 20.0;
		double importance = nodeStat.importance;

		long colorBrightness = Math.round(importance * risk * 85.0);
		Color color = new Color(null, 170 + (int)colorBrightness, 170 - (int)colorBrightness * 2, 170 - (int)colorBrightness * 2);
		shape.setBackgroundColor(color);
		return false;
	}
}
